<!--
  * 订单表
  *
  * @Author:    zhoujk
  * @Date:      2025-01-26 06:40:44
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="订单完成时间" class="smart-query-form-item">
        <a-date-picker
          :allowClear="false"
          :inputReadOnly="true"
          valueFormat="YYYY-MM-DD"
          v-model:value="queryForm.fTime"
          style="width: 120px"
          @change="onChangeTTime"
        />
        <a-button style="margin-left: 5px" @click="onChangeDay(true)">前一天</a-button>
        <a-button style="margin-left: 5px" @click="onChangeDay(false)">后一天</a-button>
      </a-form-item>
      <a-form-item label="订单号" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.id" placeholder="订单号" />
      </a-form-item>
      <a-form-item label="门店名称" class="smart-query-form-item">
        <StorePageSelect v-model:value="queryForm.storeId" :placeholder="'请选择'" width="200px" />
      </a-form-item>
      <a-form-item label="收银员" class="smart-query-form-item">
        <StoreStaffPageSelect v-model:value="queryForm.storeStaffId" :placeholder="'请选择'" width="200px" />
      </a-form-item>
      <!-- <a-form-item label="支付通道" class="smart-query-form-item">
        <DictSelect style="width: 200px" placeholder="支付通道" v-model:value="queryForm.channelId" code="CHANNEL_BUSINESS_TYPE" />
      </a-form-item> -->
      <a-form-item label="交易类型" class="smart-query-form-item">
        <SmartEnumSelect width="200px" v-model:value="queryForm.payType" enumName="ZZB_PAY_TYPE_ENUM" placeholder="交易类型" />
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="onSearch">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block"></div>
      <div class="smart-table-setting-block">
        <!-- <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.OPERATION.ORDER" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ text, record, column }">
        <!-- 有图片预览时 注释解开并把下面的'picture'修改成自己的图片字段名即可 -->
        <!-- <template v-if="column.dataIndex === 'picture'">
                  <FilePreview :fileList="text" type="picture" />
                </template> -->

        <!-- 使用字典时 注释解开并把下面的'dict'修改成自己的字典字段名即可 有多个字典字段就复制多份同理修改 不然不显示字典 -->
        <!-- 方便修改tag的颜色 orange green purple success processing error default warning -->
        <!-- <template v-if="column.dataIndex === 'dict'">
                <a-tag color="cyan">
                  {{ text && text.length > 0 ? text.map((e) => e.valueName).join(',') : '暂无' }}
                </a-tag>
              </template> -->

        <template v-if="column.dataIndex === 'id'">
          <div style="display: flex; justify-content: space-between; align-items: center" @click="copy(text)">
            <div>{{ text }}</div>
            <component :is="$antIcons['CopyOutlined']" />
          </div>
        </template>

        <template v-if="column.dataIndex === 'payType'">
          <span>{{ $smartEnumPlugin.getDescByValue('ZZB_PAY_TYPE_ENUM', text) }}</span>
        </template>

        <template v-if="column.dataIndex === 'payWay'">
          <span>{{ selectDictLabel(ZZB_PAY_WAY, text) }}</span>
        </template>

        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="refundForm(record)" type="link">退款</a-button>
          </div>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>

  <RefundModal ref="formRef" @success="queryData" />
</template>
<script setup>
  import { onMounted, reactive, ref, getCurrentInstance } from 'vue'
  import { message } from 'ant-design-vue'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import { TABLE_ID_CONST } from '/@/constants/support/table-id-const.js'
  import { orderApi } from '/@/api/business/b-merchant/order/order-api'
  import RefundModal from '/@/components/business/comment/refund/refund-modal.vue'
  import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue'
  import StorePageSelect from '/@/components/business/b-merchant/store-page-select/index.vue'
  import StoreStaffPageSelect from '/@/components/business/b-merchant/store-staff-page-select/index.vue'
  import dayjs from 'dayjs'
  import DictSelect from '/@/components/business/comment/DataDictionariesSelect.vue'
  import { formatToMoney, selectDictLabel } from '/@/utils/common'
  import { ZZB_PAY_WAY } from '/@/constants/dict'
  const { proxy } = getCurrentInstance()

  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '交易时间',
      dataIndex: 'fTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      width: 180,
      ellipsis: true,
    },
    {
      title: '交易订单号',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '门店简称',
      dataIndex: 'storeAbbrName',
      ellipsis: true,
    },
    {
      title: '收银员',
      dataIndex: 'storeStaffName',
      ellipsis: true,
    },
    {
      title: '支付通道',
      dataIndex: 'channelName',
      ellipsis: true,
    },
    {
      title: '交易类型',
      dataIndex: 'payType',
      ellipsis: true,
    },
    {
      title: '支付方式',
      dataIndex: 'payWay',
      customRender: ({ text }) => selectDictLabel(ZZB_PAY_WAY, text),
      ellipsis: true,
    },
    {
      title: '交易金额',
      dataIndex: 'tradeAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '退款金额',
      dataIndex: 'refundAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '实收金额',
      dataIndex: 'settleAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '结算金额',
      dataIndex: 'settleAmt',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 90,
    },
  ])

  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    fTime: dayjs().startOf('day'),
    fTimeBegin: dayjs().startOf('day').valueOf(), //前一天
    fTimeEnd: dayjs().endOf('day').valueOf(), //前一天
    id: undefined, //订单号
    storeId: undefined, //门店编号
    storeStaffId: undefined, //门店店员编号
    channelId: undefined, //支付通道
    payType: undefined, //交易类型
    pageNo: 1,
    pageSize: 10,
  }
  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await orderApi.page(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }

  function onChangeDay(flag) {
    if (flag) {
      queryForm.fTime = dayjs(queryForm.fTime).subtract(1, 'days')
    } else {
      queryForm.fTime = dayjs(queryForm.fTime).add(1, 'days')
    }
    onChangeTTime(queryForm.fTime)
  }

  function onChangeTTime(dates) {
    queryForm.fTimeBegin = dayjs(dates).startOf('day').valueOf()
    queryForm.fTimeEnd = dayjs(dates).endOf('day').valueOf()
  }

  onMounted(queryData)

  // ----------------- 配置信息操作 -----------------
  function copy(content) {
    // 创建元素用于复制
    const aux = document.createElement('input')
    // 设置元素内容
    aux.setAttribute('value', content)
    // 将元素插入页面进行调用
    document.body.appendChild(aux)
    // 复制内容
    aux.select()
    // 将内容复制到剪贴板
    document.execCommand('copy')
    // 删除创建元素
    document.body.removeChild(aux)

    message.success('复制成功')
  }

  // ---------------------------- 退款 ----------------------------
  const formRef = ref()

  function refundForm(data) {
    formRef.value.show(data)
  }
</script>
